package foodmaterial.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import foodmaterial.config.ChoiceDataSource;
import foodmaterial.dto.*;
import foodmaterial.entity.*;
import foodmaterial.excel.Excel;
import foodmaterial.service.FinancialService;
import foodmaterial.service.InitService;
import foodmaterial.utils.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/financial")
@CrossOrigin(allowCredentials = "true")
public class FinancialAction extends BaseAction {

    @Autowired
    private FinancialService service ;

    @Autowired
    private InitService initService ;

    @Autowired
    private Excel excel ;

    @PostMapping("/queryProfitAndLoss")
    @ChoiceDataSource
    public JsonResult<PageInfo<StockBillDetail>> queryStockDetail(@RequestBody StockBillDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<StockBillDetail> result = service.queryStockDetail(dto) ;
        PageInfo<StockBillDetail> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/downloadProfitAndLoss")
    @ChoiceDataSource
    public void downloadProfitAndLoss(StockBillDto dto, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        excel.downloadProfitAndLossExcel(dto,
                Constants.EXCEL_PROFIT_LOSS_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/printProfitAndLoss")
    public ModelAndView printProfitAndLoss(StockBillDto dto, HttpServletRequest request) {
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue()) ;
        return new ModelAndView("printProfitAndLoss","params",dto);
    }

    @PostMapping("/purchaseCheck")
    @ChoiceDataSource
    public JsonResult<PageInfo<PurchaseBill>> queryPurchaseBill(@RequestBody PurchaseBillDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<PurchaseBill> result = service.queryPurchaseBill(dto) ;
        PageInfo<PurchaseBill> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/downloadPurchaseCheck")
    @ChoiceDataSource
    public void downloadPurchaseCheck(PurchaseBillDto dto,
            HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        excel.downloadPurchaseCheckExcel(dto,
                Constants.EXCEL_PURCHASE_CHECK_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/printPurchaseCheck")
    public ModelAndView printPurchaseCheck(PurchaseBillDto dto) {
        SysUser userDetails = this.getSysUser();
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue()) ;
        dto.setCompanyName(userDetails.getCompanyName()) ;
        return new ModelAndView("printPurchaseCheck","params",dto);
    }

    @PostMapping("/queryPurchaseSummary")
    @ChoiceDataSource
    public JsonResult<PurchaseSummaryResult> queryPurchaseSummary(@RequestBody PurchaseBillDto dto) {
        PurchaseSummaryResult psr = new PurchaseSummaryResult() ;
        psr.setTotalMoney(service.queryPurchaseSummaryTotalMoney(dto));
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<PurchaseBillDetails> result = service.queryPurchaseSummary(dto) ;
        PageInfo<PurchaseBillDetails> pageInfo = new PageInfo<>(result) ;
        psr.setPage(pageInfo);
        return new JsonResult(true, ResultCode.SUCCESS,psr) ;
    }

    @GetMapping("/downloadPurchaseSummary")
    @ChoiceDataSource
    public void downloadPurchaseSummary(PurchaseBillDto dto, HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        excel.downloadFinancialPurchaseSummaryExcel(dto,
                Constants.EXCEL_FINANCIAL_PURCHASE_SUMMARY_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/printPurchaseSummary")
    public ModelAndView printPurchaseSummary(PurchaseBillDto dto, HttpServletRequest request) {
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue()) ;
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        return new ModelAndView("printFinancialPurchaseSummary","params",dto);
    }

    @PostMapping("/queryBillingDetailedList")
    @ChoiceDataSource
    public JsonResult<PageInfo<OrdersDetail>> queryBillingDetailedList(@RequestBody CustomerBillingDetailedListDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<OrdersDetail> result = service.queryBillingDetailedList(dto) ;
        PageInfo<OrdersDetail> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @PostMapping("/queryRecipeBillingDetailedList")
    @ChoiceDataSource
    public JsonResult<PageInfo<OrdersRecipe>> queryRecipeBillingDetailedList(@RequestBody CustomerBillingDetailedListDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<OrdersRecipe> result = service.queryRecipeBillingDetailedList(dto) ;
        PageInfo<OrdersRecipe> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/downloadCustomerBillingDetailedList")
    @ChoiceDataSource
    public void downloadCustomerBillingDetailedList(CustomerBillingDetailedListDto dto,
            HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        excel.downloadCustomerBillingDetailedListExcel(dto,
                Constants.EXCEL_CUSTOMER_BILLING_DETAILED_LIST_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/downloadRecipeCustomerBillingDetailedList")
    @ChoiceDataSource
    public void downloadRecipeCustomerBillingDetailedList(CustomerBillingDetailedListDto dto,
                                                    HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        excel.downloadRecipeCustomerBillingDetailedListExcel(dto,
                Constants.EXCEL_RECIPE_CUSTOMER_BILLING_DETAILED_LIST_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/printCustomerBillingDetailedList")
    public ModelAndView printBillingDetailedList(CustomerBillingDetailedListDto dto) {
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue()) ;
        return new ModelAndView("printCustomerBillingDetailedList","params",dto);
    }

    @GetMapping("/printRecipeCustomerBillingDetailedList")
    public ModelAndView printRecipeBillingDetailedList(CustomerBillingDetailedListDto dto) {
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue()) ;
        return new ModelAndView("printRecipeCustomerBillingDetailedList","params",dto);
    }

    @PostMapping("/queryCustomerCheck")
    @ChoiceDataSource
    public JsonResult<PageInfo<Orders>> queryCheckBill(@RequestBody CustomerBillingDetailedListDto dto) {
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<Orders> result = service.queryCustomerCheck(dto) ;
        PageInfo<Orders> pageInfo = new PageInfo<>(result) ;
        return new JsonResult(true, ResultCode.SUCCESS,pageInfo) ;
    }

    @GetMapping("/downloadCustomerCheck")
    @ChoiceDataSource
    public void downloadCustomerCheck(CustomerBillingDetailedListDto dto,
            HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        SysUser userDetails = this.getSysUser();
        dto.setCompanyName(userDetails.getCompanyName()) ;
        excel.downloadCustomerCheckExcel(dto,
                Constants.EXCEL_CUSTOMER_CHECK_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/printCustomerCheck")
    public ModelAndView printCustomerCheck(CustomerBillingDetailedListDto dto) {
        SysUser userDetails = this.getSysUser() ;
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue()) ;
        dto.setCompanyName(userDetails.getCompanyName()) ;
        return new ModelAndView("printCustomerCheck","params",dto);
    }

    @GetMapping("/downloadStock")
    @ChoiceDataSource
    public void downloadStock(
            FoodMaterialDto dto,HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        excel.downloadStockExcel(dto,
                Constants.EXCEL_STOCK_TEMPLATE,response.getOutputStream());
    }

    @PostMapping("/querySaleSummary")
    @ChoiceDataSource
    public JsonResult<SaleSummaryResult> querySaleSummary(@RequestBody SaleSummaryDto dto) {
        SaleSummaryResult ssr = new SaleSummaryResult() ;
        ssr.setTotalMoney(service.querySaleSummaryTotalMoney(dto));
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<OrdersDetail> result = service.querySaleSummary(dto) ;
        PageInfo<OrdersDetail> pageInfo = new PageInfo<>(result) ;
        ssr.setPage(pageInfo);
        return new JsonResult(true, ResultCode.SUCCESS,ssr) ;
    }

    @PostMapping("/queryRecipeSaleSummary")
    @ChoiceDataSource
    public JsonResult<RecipeSaleSummaryResult> querySaleSummary(@RequestBody OrdersRecipeDto dto) {
        RecipeSaleSummaryResult ssr = new RecipeSaleSummaryResult() ;
        ssr.setTotalMoney(service.queryRecipeSaleSummaryTotalMoney(dto));
        PageHelper.startPage(dto.getPageNo(),dto.getPageSize()) ;
        List<OrdersRecipe> result = service.queryRecipeSaleSummary(dto) ;
        PageInfo<OrdersRecipe> pageInfo = new PageInfo<>(result) ;
        ssr.setPage(pageInfo);
        return new JsonResult(true, ResultCode.SUCCESS,ssr) ;
    }

    @GetMapping("/downloadSaleSummary")
    @ChoiceDataSource
    public void downloadSaleSummary(SaleSummaryDto dto,HttpServletRequest request,
            HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        excel.downloadSaleSummaryExcel(dto,
                Constants.EXCEL_SALE_SUMMARY_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/downloadRecipeSaleSummary")
    @ChoiceDataSource
    public void downloadRecipeSaleSummary(OrdersRecipeDto dto,HttpServletRequest request,
                                    HttpServletResponse response) throws IOException {
        setDownload(response,excel) ;
        excel.downloadRecipeSaleSummaryExcel(dto,
                Constants.EXCEL_RECIPE_SALE_SUMMARY_TEMPLATE,response.getOutputStream());
    }

    @GetMapping("/printSaleSummary")
    public ModelAndView printSaleSummary(SaleSummaryDto dto,HttpServletRequest request) {
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue());
        if (request.getParameter("originalProcess") != null)
            dto.setOriginalProcess(Boolean.valueOf(request.getParameter("originalProcess")));
        return new ModelAndView("printSaleSummary","params",dto);
    }

    @GetMapping("/printRecipeSaleSummary")
    public ModelAndView printRecipeSaleSummary(OrdersRecipeDto dto,HttpServletRequest request) {
        dto.setBackAddress(initService.querySysParamsByCode(Constants.BACK_SERVER_ADDRESS).getParamValue());
        return new ModelAndView("printRecipeSaleSummary","params",dto);
    }

}
